#include <jni.h>
#include <string>
#include "ZYSFFmpeg.h"
#include "ZYSCallJava.h"


extern "C" {
#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
#include <libavfilter/avfilter.h>
#include <libswscale/swscale.h>
#include "libswresample/swresample.h"
}

ZYSFFmpeg *ffmpeg = nullptr;
ZYSCallJava *callJava = nullptr;
ZYSPlayStatus *playStatus = nullptr;
_JavaVM *javaVM = nullptr;
bool isExit = true;
//java虚拟机
extern "C"
JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM *vm, void *reserved) {
    jint result = -1;
    javaVM = vm;
    JNIEnv *env;
    if (vm->GetEnv((void **) &env, JNI_VERSION_1_4) != JNI_OK) {
        return result;
    }
    return JNI_VERSION_1_4;

}

extern "C"
JNIEXPORT void JNICALL
Java_com_zysdev_zysplayer_player_ZYSPlayer_prepareNative(JNIEnv *env, jobject instance,
                                                         jstring source_) {
    const char *source = env->GetStringUTFChars(source_, 0);

    if (ffmpeg == nullptr) {
        if (callJava == nullptr) {
            callJava = new ZYSCallJava(javaVM, env, &instance);
        }
        playStatus = new ZYSPlayStatus();
        ffmpeg = new ZYSFFmpeg(playStatus, callJava, source);
        ffmpeg->prepared();
    }

}
extern "C"
JNIEXPORT void JNICALL
Java_com_zysdev_zysplayer_player_ZYSPlayer_playNative(JNIEnv *env, jobject thiz) {
    if (ffmpeg != nullptr) {
        ffmpeg->startDecode();
    }
}
extern "C"
JNIEXPORT void JNICALL
Java_com_zysdev_zysplayer_player_ZYSPlayer_pauseNative(JNIEnv *env, jobject thiz) {
    if (ffmpeg != nullptr) {
        ffmpeg->pause();
    }
}
extern "C"
JNIEXPORT void JNICALL
Java_com_zysdev_zysplayer_player_ZYSPlayer_resumeNative(JNIEnv *env, jobject thiz) {
    if (ffmpeg != nullptr) {
        ffmpeg->resume();
    }
}
extern "C"
JNIEXPORT void JNICALL
Java_com_zysdev_zysplayer_player_ZYSPlayer_muteNative(JNIEnv *env, jobject thiz, jint mute) {
    if (ffmpeg != nullptr) {
        ffmpeg->setMute(mute);
    }
}
extern "C"
JNIEXPORT void JNICALL
Java_com_zysdev_zysplayer_player_ZYSPlayer_seekNative(JNIEnv *env, jobject thiz, jint parent) {
    if (ffmpeg != nullptr) {
        ffmpeg->seek(parent);
    }
}
extern "C"
JNIEXPORT void JNICALL
Java_com_zysdev_zysplayer_player_ZYSPlayer_volumeNative(JNIEnv *env, jobject thiz, jint parent) {
    if (ffmpeg != nullptr) {
        ffmpeg->setVolume(parent);
    }
}



extern "C"
JNIEXPORT void JNICALL
Java_com_zysdev_zysplayer_player_ZYSPlayer_releaseNative(JNIEnv *env, jobject thiz) {
    if (!isExit) {
        return;
    }
    isExit = false;
    if (ffmpeg != nullptr) {
        ffmpeg->release();
        delete (ffmpeg);

        if (callJava != nullptr) {
            delete (callJava);
            callJava = nullptr;
        }
        if (playStatus != nullptr) {
            delete (playStatus);
            playStatus = nullptr;
        }
    }

}

extern "C"
JNIEXPORT void JNICALL
Java_com_zysdev_zysplayer_player_ZYSPlayer_speedNative(JNIEnv *env, jobject thiz, jfloat speed) {
    if (ffmpeg != nullptr) {
        ffmpeg->setSpeed(speed);
    }
}
extern "C"
JNIEXPORT void JNICALL
Java_com_zysdev_zysplayer_player_ZYSPlayer_pitchNative(JNIEnv *env, jobject thiz, jfloat pitch) {
    if (ffmpeg != nullptr) {
        ffmpeg->setPitch(pitch);
    }
}